﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：徐彦龙 
    //
    // 文件名：DistributePlanList.cs
    // 文件功能描述：配送计划管理：提供新增配送计划、修改配送计划、删除配送计划
    // 创建标识：徐彦龙2009-03-19
    //
    // 修改标识：
    // 修改描述：采购项目列表 2009-6-4 李刚
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
// 审核时间：
// 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;
using HZ.DrugTrade.Web;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.Model.Enum;
using System.Data.SqlClient;

namespace HZ.DrugTrade.Web.Admin
{
    public partial class DistributePlanList : BasePage
    {
        IBLL_ProcurementBasic ipb = Factory.BLL_ProcurementBasic();
        public DistributePlanList()
        {
            this.ModuleNo = Module.采购项目管理.ToString();
            this.PageOperateType = OperateType.采购项目列表.ToString();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.SetBind();
            }
        }

        /// <summary>
        /// 绑定所有配送计划
        /// </summary>
        private void SetBind()
        {
            string strDistriPlanName = this.txtDistriPlanName.Text.Trim();
            ProcurementProject procurementProject = new ProcurementProject();
            procurementProject.ProjectName = strDistriPlanName;
            if (!string.IsNullOrEmpty(txtProcureStartTimeFrom.Text.Trim()))
            {
                procurementProject.ProcureStartTimeFrom = Convert.ToDateTime(txtProcureStartTimeFrom.Text.Trim());
            }
            else
            {
                procurementProject.ProcureStartTimeFrom = Convert.ToDateTime("1890-01-01");
            }
            if (!string.IsNullOrEmpty(txtProcureStartTimeTo.Text.Trim()))
            {
                procurementProject.ProcureStartTimeTo = Convert.ToDateTime(txtProcureStartTimeTo.Text.Trim());
            }
            else
            {
                procurementProject.ProcureStartTimeTo = Convert.ToDateTime("2099-12-31");
            }
            if (!string.IsNullOrEmpty(txtProcureEndTimeFrom.Text.Trim()))
            {
                procurementProject.ProcureEndTimeFrom = Convert.ToDateTime(txtProcureEndTimeFrom.Text.Trim());
            }
            else
            {
                procurementProject.ProcureEndTimeFrom = Convert.ToDateTime("2000-01-01");
            }
            if (!string.IsNullOrEmpty(txtProcureEndTimeTo.Text.Trim()))
            {
                procurementProject.ProcureEndTimeTo = Convert.ToDateTime(txtProcureEndTimeTo.Text.Trim());
            }
            else
            {
                procurementProject.ProcureEndTimeTo = Convert.ToDateTime("2099-12-31");
            }


            //AspNetPager1.PageSize = Constants.GRIDVIEW_PAGE_SIZE;
            int totalCount = 0;

            IList<ProcurementProject> procurementProjectList = ipb.GetProjectByWhere(procurementProject, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalCount);
            this.gvwProcurementProjectList.DataSource = procurementProjectList;
            this.gvwProcurementProjectList.DataBind();
        }
        /// <summary>
        /// 新增配送计划，转向新的页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAddNewPlan_Click(object sender, EventArgs e)
        {
            HZ.Web.WebUtil.RedirectByScript("DistributePlanAdd.aspx");
        }

        /// <summary>
        /// 查询配送计划
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSelect_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            SetBind();
        }
        /// <summary>
        /// 删除配送计划
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwProcurementProjectList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("Del"))
            {
                string strProjectID = e.CommandArgument.ToString();

                int rtn = 0;
                //try
                //{
                    rtn = ipb.DeleteProcurementProjectByID(strProjectID);
                    if (rtn == 0)
                    {
                        PageError("删除失败！", "");
                    }
                    else
                        if (rtn == 1)
                        {
                            SetBind();
                            PageInfo("删除成功！", "");
                        }
                        else
                            if (rtn == 2)
                            {
                                SetBind();
                                PageError("采购项目编号不存在！", "");
                            }
                            else
                                if (rtn == 3)
                                {
                                    SetBind();
                                    PageError("该项目已有在使用的数据，无法删除！", "");                                   
                                }

                //}
                //catch (SqlException)
                //{
                //    HZ.Web.WebUtil.Alert("该项目已有在使用的数据，无法删除！", this);
                //}
            }
        }

        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            btnSelect_Click(sender, e);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwProcurementProjectList_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvwProcurementProjectList.EditIndex = e.NewEditIndex;
            SetBind();
        }

        /// <summary>
        /// 取消更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwProcurementProjectList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvwProcurementProjectList.EditIndex = -1;
            SetBind();
        }

        /// <summary>
        /// 更新采购项目
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwProcurementProjectList_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            TextBox txtProjectName = (TextBox)gvwProcurementProjectList.Rows[e.RowIndex].FindControl("txtProjectName");
            TextBox txtStartTime = (TextBox)gvwProcurementProjectList.Rows[e.RowIndex].FindControl("txtProcureStartTime");
            TextBox txtEndTime = (TextBox)gvwProcurementProjectList.Rows[e.RowIndex].FindControl("txtProcureEndTime");
            //采购项目名称不能为空
            if (string.IsNullOrEmpty(txtProjectName.Text.Trim()))
            {
                HZ.Web.WebUtil.Alert("采购项目名称不能为空！", this);
                txtProjectName.Focus();
                return;
            }
            //采购时间不能为空
            if (string.IsNullOrEmpty(txtStartTime.Text.Trim()) || string.IsNullOrEmpty(txtEndTime.Text.Trim()))
            {
                HZ.Web.WebUtil.Alert("采购时间不能为空！", this);
                txtStartTime.Focus();
                return;
            }
            //采购开始时间晚于结束时间
            if (Convert.ToDateTime(txtStartTime.Text.Trim()) > Convert.ToDateTime(txtEndTime.Text.Trim()))
            {
                HZ.Web.WebUtil.Alert("采购开始时间晚于结束时间！", this);
                txtStartTime.Text.Trim();
                return;
            }
            //更新采购项目
            ProcurementProject procurmentProject = new ProcurementProject();
            procurmentProject.ProjectID = gvwProcurementProjectList.DataKeys[e.RowIndex].Value.ToString();
            procurmentProject.ProjectName = txtProjectName.Text.Trim();
            procurmentProject.ProcureStartTime = Convert.ToDateTime(txtStartTime.Text.Trim());
            procurmentProject.ProcureEndTime = Convert.ToDateTime(txtEndTime.Text.Trim());
            if (ipb.UpdateProcurementProject(procurmentProject) == 1)
            {
                gvwProcurementProjectList.EditIndex = -1;
                SetBind();
            }
            else
            {
                PageError("修改失败！", "");
            }
        }
    }
}
